package cn.edu.nuc.creditSystem.controller.credit;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;

import cn.edu.nuc.creditSystem.bean.credit.Capital;
import cn.edu.nuc.creditSystem.bean.user.User;
import cn.edu.nuc.creditSystem.controller.BaseController;
import cn.edu.nuc.creditSystem.service.BaseService;
import cn.edu.nuc.creditSystem.service.credit.service.AccountService;
import cn.edu.nuc.creditSystem.service.credit.service.CapitalService;
import cn.edu.nuc.creditSystem.utils.PageBean;

@Controller
@RequestMapping("/cs/credit/capital")
public class CapitalController extends BaseController<Capital>{
	@Autowired
	private CapitalService cs;
	@Autowired
	private AccountService as;
	@Override
	protected BaseService<Capital> getService() {
		return cs;
	}

	@Override
	protected String getPrefix() {
		return "/credit/capital/";
	}
	
	@RequestMapping(value = "/mylist", produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public String list(Capital query, @RequestParam(value="page", required=false) Integer pageNumber,
			@RequestParam(value="rows", required=false) Integer pageSize, Model model,HttpSession session) {
		PageBean page = null;
		if(pageNumber != null) {
			page = new PageBean();
			page.setCurrPage(pageNumber);
			page.setPageSize(pageSize);
		}
		User user=(User)session.getAttribute("user");
		query.setUserid(user.getId());
		List<Capital> list = getService().query(query, page);
		Gson gson = new Gson();
		String json = gson.toJson(list);
		
		if(pageNumber != null) {
			json = "{\"rows\":" + json + ",\"total\":" + page.getTotal() + "}";
		}
		return json;
	}
	@RequestMapping("/mycredit")
	private String MyCount(HttpSession session,Model model){
		User user=(User)session.getAttribute("user");
		model.addAttribute("account",as.get(user.getId()));
		return "/credit/capital/myCapital";
	}
}
